//
//  ShopViewController.m
//  A段项目
//
//  Created by ～whc on 15-6-17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ShopViewController.h"

@interface ShopViewController ()

@end

@implementation ShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor greenColor]];
    self.navigationItem.title = @"商城";
    
    
    
    self.imageBack = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yd__bg3.jpg"]];
    self.imageBack.frame = [[UIScreen mainScreen]bounds];
    //[self.view addSubview:self.imageBack];
    [_imageBack release];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40 +24, WIDTH, HEIGHT-20-44-49)  collectionViewLayout:flowLayout];
    // self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[ShopCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView setBackgroundView:self.imageBack];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //item 大小
    flowLayout.itemSize = CGSizeMake(160, 270);
    //行间距
    flowLayout.minimumLineSpacing = 5;
    //列间距
    flowLayout.minimumInteritemSpacing = 0;
    //title高
    flowLayout.headerReferenceSize = CGSizeMake(0, 40);
    //边距
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
    [self afnGetRequest];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];
    [_HUD autorelease];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-saomiaoerweima.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barButton:)];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"12" style:UIBarButtonItemStylePlain target:self action:@selector(barButton:)];
    
}
-(void)barButton:(UIButton *)button
{
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //    return 2;
    return self.array.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    
    goods *good = [self.array objectAtIndex:indexPath.row];
    cell.good = good;
    
    
    return cell;
    
}

-(void)afnGetRequest
{
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url = @"http://mall.wv.mtime.cn/Service/callback.mi/ECommerce/RecommendProducts.api?t=201562215154352430&goodsIds=100843%2C100427%2C100416&pageIndex=1";
    NSString *url_string = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableArray *array = [NSMutableArray array];
        self.array = [NSMutableArray array];
        array = [responseObject objectForKey:@"goodsList"];
        
        for (NSMutableDictionary *dic in array) {
            goods *good = [[goods alloc]init];
            good.image = [dic objectForKey:@"image"];
            good.name = [dic objectForKey:@"name"];
            good.minSalePrice = [[dic objectForKey:@"minSalePrice"]integerValue];;
            [self.array addObject:good];
            [good release];
            
        }
        [self.collectionView reloadData];
        [self.HUD hide:YES];

        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        [self.HUD hide:YES];

    }];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
